import time

from DrissionPage import ChromiumPage, ChromiumOptions
from app.const import IS_LINUX

# DefaultUserAgent = "ChatGPT/1.2024.206 (Mac OS X 14.4.1; arm64; build 1722395003)"
DefaultUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"


class AuthBrowser():
    def __init__(self):
        browser_path = "/usr/bin/google-chrome"
        options = ChromiumOptions()
        # options.set_paths(browser_path=browser_path)

        if DefaultUserAgent:
            options.set_user_agent(DefaultUserAgent)

        arguments = [
            "--accept-lang=zh-CN,zh;q=0.9,en;q=0.8",
            "--no-first-run",
            "--force-color-profile=srgb",
            "--metrics-recording-only",
            "--password-store=basic",
            "--use-mock-keychain",
            "--export-tagged-pdf",
            "--no-default-browser-check",
            "--enable-features=NetworkService,NetworkServiceInProcess,LoadCryptoTokenExtension,PermuteTLSExtensions",
            "--disable-gpu",
            "--disable-infobars",  # 关闭菜单栏
            "--disable-extensions",
            "--disable-popup-blocking",
            "--disable-background-mode",
            "--disable-features=FlashDeprecationWarning,EnablePasswordsAccountStorage,PrivacySandboxSettings4",
            "--deny-permission-prompts",
            "--disable-suggestions-ui",
            "--hide-crash-restore-bubble",
            "--window-size=1920,1080",
        ]

        if IS_LINUX:
            arguments.append("--start-maximized")
            arguments.append("--no-sandbox")
            options.headless(True)

        else:
            pass
            # arguments.append("--start-fullscreen")

        for argument in arguments:
            options.set_argument(argument)

        self.driver = ChromiumPage(addr_or_opts=options)
        self.driver.set.cookies.clear()

    def login(self, username, password):
        url = "https://chatgpt.com/"

        tab_id = self.driver.new_tab(url).tab_id
        tab = self.driver.get_tab(tab_id)
        tab.wait.doc_loaded()

        login_btn = tab.ele('css=button[data-testid="login-button"]:not([as])')
        print('login_btn', login_btn)
        login_btn.click()

        tab.wait.url_change("https://auth.openai.com")

        login_url = tab.url.replace('https://auth.openai.com', 'https://auth0.openai.com')
        # print("login_url", login_url)
        tab.get(login_url)
        tab.wait.url_change("https://auth0.openai.com")
        time.sleep(2)
        username_ele = tab.ele('@name=username')
        username_ele.input(username)

        button_ele1 = tab.ele('css=button[name="action"][data-action-button-primary="true"]')
        button_ele1.click()

        password_ele = tab.ele('@name=password')
        password_ele.input(password)

        tab.run_js('document.querySelector("button[name=action]").removeAttribute("disabled")')
        button_ele2 = tab.ele('@name=action')
        button_ele2.click()

        tab.wait.load_start()
        tab.wait.url_change(url)
        print(tab.cookies())
